// pages/offer/offer.js
let app = getApp();
let testAddr = app.globalData.testAddr;
let imgurl = app.globalData.imgurl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: [{
        id: "0",
        logname: "询价"
      },
      {
        id: "1",
        logname: "报价"
      }
    ],
    arr_nav: [{
        id: "0",
        logname: "全部询价"
      },
      {
        id: "1",
        logname: "我的询价"
      },
      {
        id: "2",
        logname: "收到的询价"
      },
      {
        id: "3",
        logname: "历史询价"
      },
    ],
    arr_nav_2: [{
        id: "0",
        logname: "我发布的报价"
      },
      {
        id: "1",
        logname: "我正在交易的报价"
      }
    ],
    currentTabsIndex: 0,
    currentTabsIndex_nav: 0,
    currentTabsIndex_nav_2: 0,
    page: 1,
    allList: [], //所有列表
    itemList: [], //每次加载的列表
    page_add: 1,
    addList: [], //所有列表
    additemList: [], //每次加载的列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '请稍后',
    })
    this.setData({
      guestid: ''
    })
    this.earnList()
  },
  // 询价及报价tab切换
  onTabsItemTap: function(event) {
    wx.showLoading({
      title: '请稍后',
    })
    var index = event.currentTarget.dataset.index;
    var that = this
    this.setData({
      currentTabsIndex: index
    });
    if (index == 0) {
      if (that.data.currentTabsIndex_nav == 0) {
          that.setData({
            page: 1,
            guestid: '',
            allList: []
          })
          that.earnList()
      } else if (that.data.currentTabsIndex_nav == 1) {
          var guestid = wx.getStorageSync("list").guestid;
          that.setData({
            page: 1,
            guestid: guestid,
            allList: []
          })
          that.earnList()
      } else if (that.data.currentTabsIndex_nav == 2) {
          var guestid = wx.getStorageSync("list").guestid;
          that.setData({
            page: 1,
            guestid: guestid,
            allList: []
          })
          that.receivedList()
      } else if (that.data.currentTabsIndex_nav == 3) {
          var guestid = wx.getStorageSync("list").guestid;
          that.setData({
            page: 1,
            guestid: guestid,
            allList: []
          })
          that.getHistory()
        }
    }else if (index == 1) {
      if (that.data.currentTabsIndex_nav_2 == 0) {
        var guestid = wx.getStorageSync("list").guestid;
        that.setData({
          page_add: 1,
          guestid: guestid,
          addList: []
        })
        that.earnaddList()
      } else if (that.data.currentTabsIndex_nav_2 == 1) {
        var guestid = wx.getStorageSync("list").guestid;
        that.setData({
          page_add: 1,
          guestid: guestid,
          addList: []
        })
        that.earnchangeList()
      }
    }
  },
  // 询价部分tab切换
  onnavsItemTap: function(e) {
    wx.showLoading({
      title: '请稍后',
    })
    var that = this
    var index = e.currentTarget.dataset.index;
    that.setData({
      currentTabsIndex_nav: index
    });
    if (index == 0) {
      that.setData({
        page: 1,
        guestid: '',
        allList: []
      })
      that.earnList()
    } else if (index == 1) {
      var guestid = wx.getStorageSync("list").guestid;
      that.setData({
        page: 1,
        guestid: guestid,
        allList: []
      })
      that.earnList()
    } else if (index == 2) {
      var guestid = wx.getStorageSync("list").guestid;
      that.setData({
        page: 1,
        guestid: guestid,
        allList: []
      })
      that.receivedList()
    } else if (index == 3) {
      var guestid = wx.getStorageSync("list").guestid;
      that.setData({
        page: 1,
        guestid: guestid,
        allList: []
      })
      that.getHistory()
    }
  },
  // 报价部分tab切换
  onnavsItemTap_2: function(e) {
    wx.showLoading({
      title: '请稍后',
    })
    var that = this;
    var index = e.currentTarget.dataset.index;
    this.setData({
      currentTabsIndex_nav_2: index
    });
    if (index == 0) {
      var guestid = wx.getStorageSync("list").guestid;
      that.setData({
        page_add: 1,
        guestid: guestid,
        addList: []
      })
      that.earnaddList()
    } else if (index == 1) {
      var guestid = wx.getStorageSync("list").guestid;
      that.setData({
        page_add: 1,
        guestid: guestid,
        addList: []
      })
      that.earnchangeList()
    }

  },
  // 询价单详情
  answer_datail:function(e){
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../offer/sheet/sheet?id=' + id,
    })
  },
  // 获取询价
  earnList: function() {
    var that = this
    var page = that.data.page;
    var allList = that.data.allList;
    var guestid = that.data.guestid;
    app.util.request({
      url: testAddr + "/enquiry/getDetail?pageIndex=" + page + '&guestID=' + guestid,
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data) {
          var list = res.data;
          for (var i = 0; i < list.length; i++) {
            var time = list[i].createTime
            if (time){
              list[i].createTime = time.slice(0, 10)
            }
            if (list[i].imgSrc){
              list[i].imgSrc = imgurl + 'enquiry/' + list[i].imgSrc
            }
          }
          that.setData({
            page: page + 1
          })
          allList = allList.concat(list)
          that.setData({
            itemList: list,
            allList: allList
          })
        }
      },
      fail: function(res) {

      },
      complete: function(res) {
        wx.hideLoading()
      },
    });
  },
  // 收到的询价
  receivedList: function() {
    var that = this
    var page = that.data.page;
    var allList = that.data.allList;
    var guestid = that.data.guestid;
    app.util.request({
      url: testAddr + "/enquiry/getOtherList?pageIndex=" + page + '&guestID=' + guestid,
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data) {
          var list = res.data;
          for (var i = 0; i < list.length; i++) {
            var time = list[i].createTime
            if (time) {
              list[i].createTime = time.slice(0, 10)
            }
            if (list[i].imgSrc){
              list[i].imgSrc = imgurl + 'enquiry/' + list[i].imgSrc
            }
          }
          that.setData({
            page: page + 1
          })
          allList = allList.concat(list)
          that.setData({
            itemList: list,
            allList: allList
          })
        }
      },
      fail: function(res) {

      },
      complete: function(res) {
        wx.hideLoading()
      },
    });
  },
  // 获取历史询价
  getHistory: function() {
    var that = this
    var page = that.data.page;
    var allList = that.data.allList;
    var guestid = that.data.guestid;
    app.util.request({
      url: testAddr + "/enquiry/getHistory?pageIndex=" + page + '&guestID=' + guestid,
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data) {
          var list = res.data;
          for (var i = 0; i < list.length; i++) {
            var time = list[i].createTime
            if (time) {
              list[i].createTime = time.slice(0, 10)
            }
            if (list[i].imgSrc) {
              list[i].imgSrc = imgurl + 'enquiry/' + list[i].imgSrc
            }
          }
          that.setData({
            page: page + 1
          })
          allList = allList.concat(list)
          that.setData({
            itemList: list,
            allList: allList
          })
        }
      },
      fail: function(res) {

      },
      complete: function(res) {
        wx.hideLoading()
      },
    });
  },
  // 获取我发布的报价
  earnaddList: function() {
    var that = this
    var page = that.data.page_add;
    var addList = that.data.addList;
    var guestid = that.data.guestid;
    app.util.request({
      url: testAddr + "/offer/getList?pageIndex=" + page + '&guestID=' + guestid,
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data) {
          var list = res.data.result;
          var jsonStr = list
          if (typeof jsonStr != 'object' && jsonStr != '') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点
            var jsonStr = JSON.parse(jsonStr);
          }
          list = jsonStr
          for (var i = 0; i < list.length; i++) {
            var time = list[i].endTime
            if (list[i].endTime) {
              list[i].endTime = list[i].endTime.slice(0, 10)
            }
            if (list[i].createTime) {
              list[i].createTime = list[i].createTime.slice(0, 10)
            }
            if (list[i].offerDetailImg) {
              list[i].offerDetailImg = imgurl + 'offer/' + list[i].offerDetailImg
            }
          }
          console.log(list)
          that.setData({
            page_add: page + 1
          })
          addList = addList.concat(list)
          that.setData({
            additemList: list,
            addList: addList
          })
        }
      },
      fail: function(res) {

      },
      complete: function(res) {
        wx.hideLoading()
      },
    });
  },
  // 获取我正在交易的报价
  earnchangeList: function() {
    var that = this
    var page = that.data.page_add;
    var addList = that.data.addList;
    var guestid = that.data.guestid;
    app.util.request({
      url: testAddr + "/offer/getDealList?pageIndex=" + page + '&guestID=' + guestid,
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data) {
          var list = res.data.result;
          var jsonStr = list
          if (typeof jsonStr != 'object' && jsonStr != '') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点
            var jsonStr = JSON.parse(jsonStr);
          }
          list = jsonStr
          for (var i = 0; i < list.length; i++) {
            var time = list[i].endTime
            if (list[i].endTime) {
              list[i].endTime = list[i].endTime.slice(0, 10)
            }
            if (list[i].createTime) {
              list[i].createTime = list[i].createTime.slice(0, 10)
            }
            if (list[i].offerDetailImg) {
              list[i].offerDetailImg = imgurl + 'offer/' + list[i].offerDetailImg
            }
          }
          console.log(list)
          that.setData({
            page_add: page + 1
          })
          addList = addList.concat(list)
          that.setData({
            additemList: list,
            addList: addList
          })
        }
      },
      fail: function(res) {

      },
      complete: function(res) {
        wx.hideLoading()
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.currentTabsIndex == 0) {
      if (that.data.currentTabsIndex_nav == 0) {
        that.setData({
          guestid: ''
        })
        that.earnList()
      } else if (that.data.currentTabsIndex_nav == 1) {
        var guestid = wx.getStorageSync("list").guestid;
        that.setData({
          guestid: guestid,
        })
        that.earnList()
      } else if (that.data.currentTabsIndex_nav == 2) {
        var guestid = wx.getStorageSync("list").guestid;
        that.setData({
          guestid: guestid,
        })
        that.receivedList()
      } else if (that.data.currentTabsIndex_nav == 3) {
        var guestid = wx.getStorageSync("list").guestid;
        that.setData({
          guestid: guestid,
        })
        that.getHistory()
      }
    } else if (that.data.currentTabsIndex == 1) {
      if (that.data.currentTabsIndex_nav_2 == 0) {
        var guestid = wx.getStorageSync("list").guestid;
        that.setData({
          guestid: guestid,
        })
        that.earnaddList()
      } else if (that.data.currentTabsIndex_nav_2 == 1) {
        var guestid = wx.getStorageSync("list").guestid;
        that.setData({
          guestid: guestid,
        })
        that.earnchangeList()
      }
    }
    this.earnList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})